using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class FaceLiftMachineItem : BaseItem
	{
		public enum Anim
		{
			Static = 0,
			MoveRight = 1,
			StayRight = 2,
			MoveLeft = 3
		}

		private float ORIGINAL_X = 0.6f;

		private float TARGET_X = 3f;

		private int MAX_EYE_ID = 10;

		private int MAX_NOSE_ID = 6;

		private int MAX_BROW_ID = 16;

		private int MAX_MOUTH_ID = 13;

		private int MAX_FEATURE_ID = 11;

		private Color32[] colors = new Color32[5]
		{
			new Color32(237, 178, 160, byte.MaxValue),
			new Color32(byte.MaxValue, 222, 194, byte.MaxValue),
			new Color32(237, 178, 160, byte.MaxValue),
			new Color32(161, 114, 84, byte.MaxValue),
			new Color32(168, 100, 77, byte.MaxValue)
		};

		private int eyeId;

		private int noseId;

		private int browId;

		private int mouthId;

		private int featureId;

		private int colorIndex;

		private Anim anim;

		private float animTimer;

		private Transform head;

		private Transform face;

		private Transform mask;

		private List<Transform> btns;

		private CharacterItem selectCharacter;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			Transform transform = model.Find("Btns");
			transform.SetLocalZ(-1E-05f);
			btns = new List<Transform>();
			int childCount = transform.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = transform.GetChild(i);
				child.gameObject.layer = 15;
				if (i > 2)
				{
					child.SetLocalZ(-1E-05f);
				}
				btns.Add(child);
			}
			head = model.Find("Head");
			head.SetLocalZ(-2E-05f);
			face = head.Find("Face");
			face.SetLocalZ(-3E-05f);
			Transform trans = face.Find("Feature");
			trans.SetLocalZ(1E-05f);
			Transform trans2 = model.Find("Body");
			trans2.SetLocalZ(-1E-05f);
			Transform trans3 = model.Find("Hair");
			trans3.SetLocalZ(-5E-05f);
			Transform trans4 = model.Find("HairBack");
			trans4.SetLocalZ(-1E-05f);
			Transform trans5 = model.Find("Displayer");
			trans5.SetLocalZ(-5E-05f);
			mask = model.Find("Mask");
			mask.SetLocalZ(2E-05f);
			InitFace(data.eyeId, data.noseId, data.browId, data.mouthId, data.featureId);
			colorIndex = data.state;
			ResetColor();
		}

		public void InitFace(int eyeId, int noseId, int browId, int mouthId, int featureId)
		{
			this.eyeId = eyeId;
			this.noseId = noseId;
			this.browId = browId;
			this.mouthId = mouthId;
			this.featureId = featureId;
			ResetEye();
			ResetNose();
			ResetBrow();
			ResetMouth();
			ResetFeature();
		}

		public void InitColor(Color32 color)
		{
			colorIndex = 0;
			for (int i = 0; i < colors.Length; i++)
			{
				if (color.Equals(colors[i]))
				{
					colorIndex = i;
					break;
				}
			}
			ResetColor();
		}

		protected override void LateUpdate()
		{
			if (anim == Anim.Static)
			{
				return;
			}
			animTimer += Time.deltaTime;
			if (anim == Anim.MoveRight)
			{
				mask.SetLocalX(Mathf.Lerp(ORIGINAL_X, TARGET_X, animTimer));
				if (animTimer >= 1f)
				{
					anim = Anim.StayRight;
					animTimer = 0f;
					Singleton<AudioManager>.Instance.PlaySound("sound_explode");
					Vector3 pos = new Vector3(selectCharacter.transform.position.x - 2f, selectCharacter.transform.position.y, base.transform.position.z);
					Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", pos, 2f);
					Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", pos, 3f);
				}
			}
			else if (anim == Anim.StayRight)
			{
				if (animTimer >= 1.5f)
				{
					anim = Anim.MoveLeft;
					animTimer = 0f;
					if (selectCharacter != null)
					{
						selectCharacter.InitFace(browId, eyeId, noseId, mouthId, featureId);
						selectCharacter.SetBodyColor(colors[colorIndex]);
						selectCharacter.SetColliderEnabled(true);
						Singleton<AudioManager>.Instance.PlaySound("sound_face_lift_machine_2");
					}
				}
			}
			else if (anim == Anim.MoveLeft)
			{
				mask.SetLocalX(Mathf.Lerp(TARGET_X, ORIGINAL_X, animTimer));
				if (animTimer >= 1f)
				{
					anim = Anim.Static;
					animTimer = 0f;
				}
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			if (anim != 0)
			{
				return;
			}
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (!(raycastHit2D.collider != null) || !(raycastHit2D.collider.transform.parent.parent == model))
			{
				return;
			}
			string text = raycastHit2D.collider.transform.name;
			if (text.Equals("Btn1"))
			{
				NextEye();
				ResetEye();
				SetBtnState(raycastHit2D.collider.transform, true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
			else if (text.Equals("Btn2"))
			{
				NextNose();
				ResetNose();
				SetBtnState(raycastHit2D.collider.transform, true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
			else if (text.Equals("Btn3"))
			{
				NextBrow();
				ResetBrow();
				SetBtnState(raycastHit2D.collider.transform, true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
			else if (text.Equals("Btn4"))
			{
				NextMouth();
				ResetMouth();
				SetBtnState(raycastHit2D.collider.transform, true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
			else if (text.Equals("Btn5"))
			{
				NextFeature();
				ResetFeature();
				SetBtnState(raycastHit2D.collider.transform, true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
			else if (text.Equals("Btn6"))
			{
				NextColor();
				ResetColor();
				SetBtnState(raycastHit2D.collider.transform, true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
			else
			{
				if (!text.Equals("BtnConfirm"))
				{
					return;
				}
				SetBtnState(raycastHit2D.collider.transform, true);
				if (childItems.Count > 0 && childItems[0].childItems.Count > 0)
				{
					for (int i = 0; i < childItems[0].childItems.Count; i++)
					{
						BaseItem baseItem = childItems[0].childItems[i];
						if (baseItem.itemData.scriptType == 1)
						{
							selectCharacter = (CharacterItem)baseItem;
							selectCharacter.SetColliderEnabled(false);
							anim = Anim.MoveRight;
							animTimer = 0f;
							Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
							Singleton<AudioManager>.Instance.PlaySound("sound_face_lift_machine_1");
							return;
						}
					}
				}
				Singleton<AudioManager>.Instance.PlaySound("sound_error");
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			for (int i = 0; i < btns.Count; i++)
			{
				SetBtnState(btns[i], false);
			}
		}

		private void SetBtnState(Transform trans, bool isDown)
		{
			trans.Find("Down").SetObjectActive(isDown);
			trans.Find("Up").SetObjectActive(!isDown);
		}

		private void NextEye()
		{
			eyeId++;
			if (eyeId > MAX_EYE_ID)
			{
				eyeId = 1;
			}
		}

		private void ResetEye()
		{
			Transform transform = Singleton<AssetBundleManager>.Instance.characterResource.transform;
			ChangeSprite(head.Find("Face/Eye"), transform.Find("Model/Body/Head/Face/Eye/" + eyeId + "/Normal"));
		}

		private void NextNose()
		{
			noseId++;
			if (noseId > MAX_NOSE_ID)
			{
				noseId = 1;
			}
		}

		private void ResetNose()
		{
			Transform transform = Singleton<AssetBundleManager>.Instance.characterResource.transform;
			ChangeSprite(head.Find("Face/Nose"), transform.Find("Model/Body/Head/Face/Nose/" + noseId));
		}

		private void NextBrow()
		{
			browId++;
			if (browId > MAX_BROW_ID)
			{
				browId = 1;
			}
		}

		private void ResetBrow()
		{
			Transform transform = Singleton<AssetBundleManager>.Instance.characterResource.transform;
			ChangeSprite(head.Find("Face/Brow"), transform.Find("Model/Body/Head/Face/Brow/" + browId));
		}

		private void NextMouth()
		{
			mouthId++;
			if (mouthId > MAX_MOUTH_ID)
			{
				mouthId = 1;
			}
		}

		private void ResetMouth()
		{
			Transform transform = Singleton<AssetBundleManager>.Instance.characterResource.transform;
			ChangeSprite(head.Find("Face/Mouth"), transform.Find("Model/Body/Head/Face/Mouth/" + mouthId + "/Normal"));
		}

		private void NextFeature()
		{
			featureId++;
			if (featureId > MAX_FEATURE_ID)
			{
				featureId = 1;
			}
		}

		private void ResetFeature()
		{
			Transform transform = Singleton<AssetBundleManager>.Instance.characterResource.transform;
			ChangeSprite(head.Find("Face/Feature"), transform.Find("Model/Body/Head/Face/Feature/" + featureId));
		}

		private void NextColor()
		{
			colorIndex++;
			if (colorIndex >= colors.Length)
			{
				colorIndex = 0;
			}
		}

		private void ResetColor()
		{
			if (colorIndex < colors.Length)
			{
				head.Find("Sprite").GetComponent<SpriteRenderer>().color = colors[colorIndex];
			}
		}

		protected void ChangeSprite(Transform trans, Transform copy)
		{
			ChangeSprite(trans.Find("Sprite").GetComponent<SpriteRenderer>(), copy);
		}

		protected void ChangeSprite(SpriteRenderer spriteRenderer, Transform copy)
		{
			if (copy != null)
			{
				SpriteRenderer component = copy.GetComponent<SpriteRenderer>();
				ChangeSprite(spriteRenderer, component);
			}
			else
			{
				spriteRenderer.sprite = null;
			}
		}

		protected void ChangeSprite(SpriteRenderer spriteRenderer, SpriteRenderer copySpriteRenderer)
		{
			if (copySpriteRenderer != null)
			{
				spriteRenderer.sprite = copySpriteRenderer.sprite;
				spriteRenderer.transform.localPosition = copySpriteRenderer.transform.localPosition;
			}
			else
			{
				spriteRenderer.sprite = null;
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["21"] = eyeId;
			obj["20"] = browId;
			obj["23"] = mouthId;
			obj["22"] = noseId;
			obj["55"] = featureId;
			obj["33"] = colorIndex;
		}
	}
}
